#!/bin/bash

set -o errexit
export LC_ALL=C

export BASE_DATA_PATH=/home/mixagol/data
# export BASE_DATE=20111030
export BASE_DATE=20120802

export AN_DIR=~/data/6_annotation/a.laidlawii_wunw
export SRC_URL="ftp://ftp.ncbi.nlm.nih.gov/genomes/Bacteria/Acholeplasma_laidlawii_PG_8A_uid58901/NC_010163.gbk"
export GENOME_ID="NC_010163"

mkdir -p ${AN_DIR}/orgs

##
## Fetch and prepare genes
##
wget ${SRC_URL} -O "${AN_DIR}/orgs/${GENOME_ID}"

echo ${BASE_DATE} > ${AN_DIR}/base_date.txt

# Только для исключения этого генома
echo "${GENOME_ID}" > ${AN_DIR}/excl_genomes.txt
# Иначе:
echo > ${AN_DIR}/excl_genomes.txt

1_databases/create_genom_db.py      ${AN_DIR}/orgs                  ${AN_DIR}/genom.db
1_databases/create_gen_desc_db.py   ${AN_DIR}/orgs                  ${AN_DIR}/gen_desc.db
1_databases/create_genes.py         ${AN_DIR}/genom.db              ${AN_DIR}/gen_desc.db       > ${AN_DIR}/genes.txt
1_databases/create_gene_info.py     ${AN_DIR}/orgs                  ${AN_DIR}/genes_info.txt
sort -t$'\t' -k1,1 -o               ${AN_DIR}/genes_info.txt        ${AN_DIR}/genes_info.txt

join -t$'\t' \
    ${AN_DIR}/genes_info.txt \
    <(join -t$'\t' -1 2 -2 1 \
        <(cat ${AN_DIR}/genes_info.txt | cut -f1,4 \
            | 1_databases/refine_gene_info_part3.py \
            | sort -t$'\t' -k2,2 -T . -S200M \
        ) \
        ${BASE_DATA_PATH}/1_databases/${BASE_DATE}/genes_info_refined_map.txt \
        | cut -f2,3 \
        | sort -t$'\t' -k1,1 -T . -S200M \
    ) \
    > ${AN_DIR}/genes_info_refined.txt

join -t$'\t' -1 2 -2 1 \
    <(join -t$'\t' -1 2 -2 1 \
        <(cat "${AN_DIR}/genes_info.txt" | cut -f1,2 | sort -t$'\t' -k2,2 -T . -S1G) \
        "${BASE_DATA_PATH}/7_aux/kegg/${BASE_DATE}/ncbi2uniprot.txt" \
        | cut -f2,3 \
        | sort -t$'\t' -k2,2 -T . -S1G \
    ) \
    <( cat "${BASE_DATA_PATH}/7_aux/gene_ontology/${BASE_DATE}//uniprot2go.txt" ) \
    | cut -f2,3 \
    | sort -t$'\t' -k1,1 -T . -S1G \
    > ${AN_DIR}/genes_go.txt


# assertions
cat ${AN_DIR}/genes_info.txt | wc -l
cat ${AN_DIR}/genes_info_refined.txt | wc -l
cat ${AN_DIR}/genes_info_refined.txt | cut -f1 | uniq | wc -l

cat ${AN_DIR}/genes_info_refined.txt | cut -f1,4 > ${AN_DIR}/gene_functions_ncbi.txt
cat ${AN_DIR}/genes_info_refined.txt | cut -f1,5 > ${AN_DIR}/gene_functions_ncbi_refined.txt

cut -f2 ${AN_DIR}/genes_info.txt | sort -t$'\t' -k1,1 | 5_pathways/create_ncbi_kegg_map.py > ${AN_DIR}/ncbi_kegg_map.txt
cut -f2 ${AN_DIR}/ncbi_kegg_map.txt | sort -t$'\t' -k1,1 | 5_pathways/get_genes_info.py > ${AN_DIR}/kegg_genes_functions.txt

join -t$'\t' -1 2 -2 1 \
    <(cat ${AN_DIR}/genes_info.txt | cut -f1,2 | sort -t$'\t' -k2,2) \
    <(cat ${AN_DIR}/ncbi_kegg_map.txt | cut -f1,2 ) \
    | cut -f2,3 \
    | sort -t$'\t' -k1,1 \
    > ${AN_DIR}/gene_kegg_map.txt
    
join -t$'\t' -1 2 -2 1 \
    <(cat ${AN_DIR}/gene_kegg_map.txt | cut -f1,2 | sort -t$'\t' -k2,2) \
    <(cat ${AN_DIR}/kegg_genes_functions.txt) \
    | cut -f2,3 \
    | sort -t$'\t' -k1,1 \
    > ${AN_DIR}/gene_functions.txt

join -t $'\t' \
    "${BASE_DATA_PATH}/5_pathways/${BASE_DATE}/gene_pathway_map.txt" \
    "${AN_DIR}/genes_go.txt" \
  | cut -f2,3 \
  | sort -t $'\t' -k1,1 -k2,2 \
  | uniq \
  > "${AN_DIR}/pathways_go.txt"

join -t$'\t' -j 2 \
    <(join -t$'\t' -1 2 -2 1 \
        <(cat "${AN_DIR}/genes_info.txt" | cut -f1,2 | sort -t$'\t' -k2,2 -T . -S1G) \
        "${BASE_DATA_PATH}/7_aux/kegg/${BASE_DATE}/ncbi2kegg.txt" \
        | cut -f2,3 \
        | sort -t$'\t' -k2,2 -T . -S1G \
    ) \
    <(join -t$'\t' \
        ${BASE_DATA_PATH}/5_pathways/${BASE_DATE}/pathway_kegg_map_full.txt \
        ${BASE_DATA_PATH}/5_pathways/${BASE_DATE}/pathways_full.txt \
        | cut -f1,2,3,5 \
        | sort -t$'\t' -k2,2 -T . -S1G \
    ) \
    | cut -f2- \
    | sort -t$'\t' -k1,1 -T . -S1G \
    > ${AN_DIR}/genes_pathways.txt


##
## Annotate genes
##

mkdir $AN_DIR/src
cat $AN_DIR/genes.txt | split -a3 -l 1 - $AN_DIR/src/

mkdir $AN_DIR/src_faa
ls $AN_DIR/src | xargs -n1 -P8 -I {} bash -c "cat $AN_DIR/src/{} | awk -F'\t' '{print \">\"\$1\"\n\"\$2}' > $AN_DIR/src_faa/{}.faa" 

# full
find $AN_DIR/src_faa/ -type f -name "*.faa" | sort | xargs -n1 -P1 -I {} bash -c "cat {} | 6_annotation/annotate_wu_nw_simple.sh $AN_DIR && rm {}"

# restrict
find $AN_DIR/ -maxdepth 1 -type d -name "annot_*" | sort | xargs -n1 -P1 6_annotation/annotate_wu_nw_simple_restrict.sh

